package be.kuleuven.cs.mop.domain.model.impl;


import static org.junit.Assert.*;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.junit.*;

import be.kuleuven.cs.mop.TestEnvironment;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;
import be.kuleuven.cs.mop.domain.model.impl.ReservationImpl;



public class ReservationImplTest {
	
	private static TestEnvironment ENV;
	
	private static long				duration	= 234567890L;
	private static ReservationImpl	res1;
	private static ReservationImpl	res2;
	
	
	
	@Before
	public void setUp() throws Exception {
		ENV = TestEnvironment.newInstance();
		
		res2 = new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum2, 300000L);
	}
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {}
	
	@After
	public void tearDown() throws Exception {}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {}
	
	@Test(expected=TaskManagerException.class)
	public void testConstructorIllegalCase1() throws TaskManagerException{
		new ReservationImpl(null, null, ENV.datum2, duration); 
	}
	
	@Test(expected=TaskManagerException.class)
	public void testConstructorIllegalCase2() throws TaskManagerException{
		new ReservationImpl(ENV.task1, ENV.resource1, null , duration); 
	}
	
	@Test(expected=TaskManagerException.class)
	public void testConstructorIllegalCase3() throws TaskManagerException{
		new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum1 , 59999L); 
	}
	
	@Test(expected=TaskManagerException.class)
	public void testConstructorIllegalCase4() throws TaskManagerException{
		final Calendar now = new GregorianCalendar();
		now.setTimeInMillis(ENV.world.getClock().getTime().getTimeInMillis() - 1);
		res1 = new ReservationImpl(ENV.task1, ENV.resource1, now , duration); 
	}
	
	@Test
	public void testConstructorLegalcase() throws TaskManagerException {
		res1 = new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum1, duration);
		assertEquals(ENV.task1,		res1.getTask());
		assertEquals(ENV.resource1,	res1.getResource());
		assertEquals(ENV.datum1,	res1.getDate());
		assertEquals(duration,		res1.getDuration());
		assertEquals(ENV.datum1.getTimeInMillis() + duration,
				res1.getEnd().getTimeInMillis());
	}
	
	@Test
	public void testCovers() throws TaskManagerException {
		res1 = new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum1 , 120000L); 
		assertFalse(res1.covers(new GregorianCalendar(2012, 5, 1, 20, 1)));
		assertTrue(res1.covers(new GregorianCalendar(2012, 5, 1, 0, 1)));
	}
	
	@Test
	public void testEquals() throws TaskManagerException {
		res1 = new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum1, ENV.duration);
		res2 = new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum1, ENV.duration);
		final ReservationImpl res =
			new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum3, ENV.duration);
		assertTrue(res1.equals(res2));
		assertFalse(res1.equals(res));
		assertFalse(res2.equals(res));
	}
	
	@Test
	public void testHasConflict() throws TaskManagerException {
		res1 = new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum1 , 12000000L);
		res2 = new ReservationImpl(ENV.task1, ENV.resource1, ENV.datum3 , 24000000L);
		assertTrue(res1.hasConflict(res2));
		assertTrue(res1.compareTo(res2) < 0);
	}
	
	@Test
	public void testValidation() {
		assertNotNull(res2.toString());
		assertFalse(res2.toString().isEmpty());
		final Calendar old = new GregorianCalendar();
		old.setTimeInMillis(System.currentTimeMillis() - 1);
		
//		assertFalse(ReservationImpl.isValidDate(null));
//		assertFalse(ReservationImpl.isValidDate(old));
		assertFalse(ReservationImpl.isValidDuration(59999L));
		assertTrue(ReservationImpl.isValidDuration(60000L));
//		assertFalse(ReservationImpl.isValidUser(null));
	}
	
}